<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>ElasticSearch | 王开琦的个人小站</title><meta name="keywords" content="ES"><meta name="author" content="王开琦"><meta name="copyright" content="王开琦"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Elasticsearch 是一个分布式的免费开源搜索和分析引擎，适用于包括文本、数字、地理空间、结构化和非结构化数据等在内的所有类型的数据。">
<meta property="og:type" content="article">
<meta property="og:title" content="ElasticSearch">
<meta property="og:url" content="https://blog.wkq.pub/blog/ElasticSearch/index.html">
<meta property="og:site_name" content="王开琦的个人小站">
<meta property="og:description" content="Elasticsearch 是一个分布式的免费开源搜索和分析引擎，适用于包括文本、数字、地理空间、结构化和非结构化数据等在内的所有类型的数据。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://blog.wkq.pub/cover/ElasticSearch.jpg">
<meta property="article:published_time" content="2022-01-01T12:13:49.000Z">
<meta property="article:modified_time" content="2022-01-01T12:13:49.000Z">
<meta property="article:author" content="王开琦">
<meta property="article:tag" content="分布式">
<meta property="article:tag" content="搜索引擎">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://blog.wkq.pub/cover/ElasticSearch.jpg"><link rel="shortcut icon" href="/media/favicon.ico"><link rel="canonical" href="https://blog.wkq.pub/blog/ElasticSearch/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: true,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'ElasticSearch',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-01-01 20:13:49'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    document.addEventListener('pjax:complete', detectApple)})(window)</script><meta name="generator" content="Hexo 5.2.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/media/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">15</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/cover/ElasticSearch.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">王开琦的个人小站</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">ElasticSearch</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-01-01T12:13:49.000Z" title="发表于 2022-01-01 20:13:49">2022-01-01</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-01-01T12:13:49.000Z" title="更新于 2022-01-01 20:13:49">2022-01-01</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%88%86%E5%B8%83%E5%BC%8F%E6%9E%B6%E6%9E%84/">分布式架构</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%88%86%E5%B8%83%E5%BC%8F%E6%9E%B6%E6%9E%84/%E5%88%86%E5%B8%83%E5%BC%8F%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E/">分布式搜索引擎</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">5.4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>22分钟</span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="搜索引擎对比"><a href="#搜索引擎对比" class="headerlink" title="搜索引擎对比"></a>搜索引擎对比</h2><ul>
<li>倒排序索引，所有搜索引擎共有的一个特点</li>
<li>Lucene是类库，只能使用Java整合，不能水平扩展</li>
<li>Solr基于Lucene</li>
<li>ES基于Lucene，分布式搜索引擎，提供近实时的搜索服务，支持PB级别的搜索，可以支持大数据的分析</li>
</ul>
<h2 id="ES核心术语"><a href="#ES核心术语" class="headerlink" title="ES核心术语"></a>ES核心术语</h2><div class="table-container">
<table>
<thead>
<tr>
<th>ES术语</th>
<th>对应关系型数据库术语</th>
</tr>
</thead>
<tbody>
<tr>
<td>ES</td>
<td>数据库</td>
</tr>
<tr>
<td>索引 index</td>
<td>表</td>
</tr>
<tr>
<td>类型 type</td>
<td>表逻辑类型</td>
</tr>
<tr>
<td>文档 document</td>
<td>行（记录）</td>
</tr>
<tr>
<td>字段 fields</td>
<td>列</td>
</tr>
<tr>
<td>映射 mapping</td>
<td>表结构定义，记录了索引的信息</td>
</tr>
<tr>
<td>近实时 NRT</td>
<td>Near real time,近实时的搜索</td>
</tr>
<tr>
<td>节点 node</td>
<td>每一个服务器</td>
</tr>
<tr>
<td>shard replica</td>
<td>数据分片和备份</td>
</tr>
</tbody>
</table>
</div>
<ul>
<li>索引在ES中可以建立多个的，其实就是一组集合，根据不同类型的业务去分不同的索引库</li>
<li>文档都以json的形式存在，一个文档就是一条记录。是一个可以被索引的最基础的数据单位，每一个索引库都是由很多文档共同构成的。</li>
<li>fields 是文档中的属性，不同的属性有不同的数据类型和值， id、name、age都是文档中的fields</li>
</ul>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">stu_index</span><br><span class="line">&#123;</span><br><span class="line">  &quot;id&quot;: 1001,</span><br><span class="line">  &quot;name&quot;: &#x27;jason&#x27;,</span><br><span class="line">  &quot;age&quot;: 19</span><br><span class="line">&#125;</span><br><span class="line">&#123;</span><br><span class="line">  &quot;id&quot;: 1002, </span><br><span class="line">  &quot;name&quot;: &#x27;tom&#x27;,</span><br><span class="line">  &quot;age&quot;: 18</span><br><span class="line">&#125;</span><br><span class="line">&#123;</span><br><span class="line">  &quot;id&quot;: 1003,</span><br><span class="line">  &quot;name&quot;: &#x27;rose&#x27;,</span><br><span class="line">  &quot;age&quot;: 22</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="集群相关"><a href="#集群相关" class="headerlink" title="集群相关"></a>集群相关</h3><p><img src= "" data-lazy-src="ES集群架构原理.png" alt="ES集群架构原理"></p>
<ul>
<li>分片（shard）:把索引库拆分成多份，分别放在不同的节点上，比如有3个节点，3个节点的所有数据内容加在一起是一个完整的索引库。分别保存到三个节点上，目的是为了水平扩容，提高吞吐量、性能</li>
<li><p>备份（replica）：每个shard的备份，防止shard数据丢失，从而达到高可用</p>
</li>
<li><p>同一个分片的主分片和副本分片不会放在同一个节点里面，因为一旦挂了就没有了</p>
</li>
</ul>
<h3 id="简称"><a href="#简称" class="headerlink" title="简称"></a>简称</h3><ul>
<li>shard = primary shard（主分片）</li>
<li>replica = replica shard （备份节点）</li>
</ul>
<blockquote>
<p>为了让多块磁盘协调工作一块运转，让所有的磁盘都物尽其用发挥全部性能，就有了磁盘阵列，磁盘阵列在电脑中称为RAID，给磁盘组建完阵列之后在电脑中只会看到一个RAID的阵列盘，它的子磁盘你是看不到的。<br>你只需要跟使用普通磁盘一样，去使用这个由多块硬盘组合起来的Raid磁盘就可以<br>raid0 会将数据平均拆分到不同的磁盘，有一块磁盘挂掉数据失效<br>radi1 也称为镜像，会将完整的数据复制到其它磁盘，radi1自动备份，是所有radi中最安全的</p>
</blockquote>
<h2 id="倒排索引"><a href="#倒排索引" class="headerlink" title="倒排索引"></a>倒排索引</h2><p>倒排索引源于实际应用中需要根据属性的值来查找记录。这种索引表中的每一项都包括一个属性值和包含该属性值的记录地址。由于不是根据记录来确定属性，而是根据属性来确定记录的位置，所以称之为倒排索引。</p>
<p><img src= "" data-lazy-src="倒排索引.png" alt="倒排索引"></p>
<h2 id="索引的一些操作"><a href="#索引的一些操作" class="headerlink" title="索引的一些操作"></a>索引的一些操作</h2><h3 id="集群健康"><a href="#集群健康" class="headerlink" title="集群健康"></a>集群健康</h3><p><a target="_blank" rel="noopener" href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_cluster_health.html#_cluster_health">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_cluster_health.html#_cluster_health</a></p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GET /_cluster/health</span><br></pre></td></tr></table></figure>
<h3 id="创建索引"><a href="#创建索引" class="headerlink" title="创建索引"></a>创建索引</h3><figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">PUT /index_test</span><br><span class="line">&#123;</span><br><span class="line">    &quot;settings&quot;:&#123;</span><br><span class="line">        &quot;index&quot;:&#123;</span><br><span class="line">            &quot;number_of_shards&quot;:&quot;2&quot;,</span><br><span class="line">            &quot;number_of_replicas&quot;:&quot;0&quot;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="查看索引"><a href="#查看索引" class="headerlink" title="查看索引"></a>查看索引</h3><figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GET _cat/indices?v</span><br></pre></td></tr></table></figure>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GET /index_test</span><br></pre></td></tr></table></figure>
<h3 id="删除索引"><a href="#删除索引" class="headerlink" title="删除索引"></a>删除索引</h3><figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DELETE /index_test</span><br></pre></td></tr></table></figure>
<h2 id="索引的mappings映射"><a href="#索引的mappings映射" class="headerlink" title="索引的mappings映射"></a>索引的mappings映射</h2><h3 id="索引分词概念"><a href="#索引分词概念" class="headerlink" title="索引分词概念"></a>索引分词概念</h3><p>index:默认true，设置为false的话，那么这个字段就不会被索引（不被索引是查询不到的）</p>
<blockquote>
<p>The option controls whether field values are indexed. It accepts or and defaults to . Fields that are not indexed are typically not queryable.</p>
</blockquote>
<h3 id="创建索引的同时创建mappings"><a href="#创建索引的同时创建mappings" class="headerlink" title="创建索引的同时创建mappings"></a>创建索引的同时创建mappings</h3><figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">PUT /index_str</span><br><span class="line">&#123;</span><br><span class="line">  &quot;settings&quot;: &#123;</span><br><span class="line">    &quot;index&quot;: &#123;</span><br><span class="line">      &quot;number_of_shards&quot;: &quot;2&quot;,</span><br><span class="line">      &quot;number_of_replicas&quot;: &quot;0&quot;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;mappings&quot;: &#123;</span><br><span class="line">    &quot;properties&quot;: &#123;</span><br><span class="line">      &quot;realname&quot;: &#123;</span><br><span class="line">        &quot;type&quot;: &quot;text&quot;,/*字符串，会被分词，会创建倒排索引*/</span><br><span class="line">        &quot;index&quot;: true  </span><br><span class="line">      &#125;,</span><br><span class="line">      &quot;username&quot;: &#123;</span><br><span class="line">        &quot;type&quot;: &quot;keyword&quot;,/*字符串，不会被分词，也不会被倒排索引；是一种精确匹配的搜索，比如说订单状态，用户的QQ号、手机号*/</span><br><span class="line">        &quot;index&quot;: false</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="查看分词效果"><a href="#查看分词效果" class="headerlink" title="查看分词效果"></a>查看分词效果</h3><figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">GET /index_str/_analyze</span><br><span class="line">&#123;</span><br><span class="line">    &quot;field&quot;:&quot;realname&quot;,</span><br><span class="line">    &quot;text&quot;:&quot;I am good&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="尝试修改-报错-如果属性已存在，则不做操作"><a href="#尝试修改-报错-如果属性已存在，则不做操作" class="headerlink" title="尝试修改(报错,如果属性已存在，则不做操作)"></a>尝试修改(报错,如果属性已存在，则不做操作)</h3><figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">POST /index_str/_mapping</span><br><span class="line">&#123;</span><br><span class="line">    &quot;properties&quot;:&#123;</span><br><span class="line">        &quot;realname&quot;:&#123;</span><br><span class="line">            &quot;type&quot;:&quot;long&quot;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="为已存在的索引创建或添加mappings"><a href="#为已存在的索引创建或添加mappings" class="headerlink" title="为已存在的索引创建或添加mappings"></a>为已存在的索引创建或添加mappings</h3><figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">POST /index_str/_mapping</span><br><span class="line">&#123;</span><br><span class="line">    &quot;properties&quot;:&#123;</span><br><span class="line">         &quot;id&quot;: &#123;</span><br><span class="line">        	&quot;type&quot;: &quot;long&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;age&quot;: &#123;</span><br><span class="line">        	&quot;type&quot;: &quot;integer&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;nickname&quot;: &#123;</span><br><span class="line">            &quot;type&quot;: &quot;keyword&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;money1&quot;: &#123;</span><br><span class="line">            &quot;type&quot;: &quot;float&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;money2&quot;: &#123;</span><br><span class="line">            &quot;type&quot;: &quot;double&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;sex&quot;: &#123;</span><br><span class="line">            &quot;type&quot;: &quot;byte&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;score&quot;: &#123;</span><br><span class="line">            &quot;type&quot;: &quot;short&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;is_teenager&quot;: &#123;</span><br><span class="line">            &quot;type&quot;: &quot;boolean&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;birthday&quot;: &#123;</span><br><span class="line">            &quot;type&quot;: &quot;date&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;relationship&quot;: &#123;</span><br><span class="line">            &quot;type&quot;: &quot;object&quot;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意：某个属性一旦被建立，就不能修改了，但是可以新增额外属性</p>
</blockquote>
<h3 id="主要数据类型"><a href="#主要数据类型" class="headerlink" title="主要数据类型"></a>主要数据类型</h3><ul>
<li>text、keyword、<del>string</del></li>
<li>long、integer、short、byte</li>
<li>double、float</li>
<li>boolean</li>
<li>date</li>
<li>object</li>
<li>数组不能混，类型一致</li>
</ul>
<blockquote>
<ul>
<li>text:文字类需要被分词被倒排索引的内容，比如<code>商品名称</code>、<code>商品详情</code>、<code>商品介绍</code>，使用text。</li>
<li>keyword：不会被分词，不会被倒排索引，直接匹配搜索，比如<code>订单状态</code>、<code>微信号</code>、<code>手机号</code>、<code>用户qq</code>，这些精确匹配，无需分词</li>
</ul>
</blockquote>
<h2 id="文档的基本操作"><a href="#文档的基本操作" class="headerlink" title="文档的基本操作"></a>文档的基本操作</h2><h3 id="添加文档与自动映射"><a href="#添加文档与自动映射" class="headerlink" title="添加文档与自动映射"></a>添加文档与自动映射</h3><figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">POST /my_doc/_doc/1 -&gt; &#123;索引名&#125;/_doc/&#123;索引ID&#125;（是指索引在es中的id，而不是这条记录的id，比如记录的id从数据库来是1001，并不是这个。如果不写，则自动生成一个字符串。建议和数据id保持一致&gt; ）</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1001,</span><br><span class="line">    &quot;name&quot;: &quot;imooc-1&quot;,</span><br><span class="line">    &quot;desc&quot;: &quot;imooc is very good, 慕课网非常牛！&quot;,</span><br><span class="line">    &quot;create_date&quot;: &quot;2019-12-24&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1002,</span><br><span class="line">    &quot;name&quot;: &quot;imooc-2&quot;,</span><br><span class="line">    &quot;desc&quot;: &quot;imooc is fashion, 慕课网非常时尚！&quot;,</span><br><span class="line">    &quot;create_date&quot;: &quot;2019-12-25&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1003,</span><br><span class="line">    &quot;name&quot;: &quot;imooc-3&quot;,</span><br><span class="line">    &quot;desc&quot;: &quot;imooc is niubility, 慕课网很好很强大！&quot;,</span><br><span class="line">    &quot;create_date&quot;: &quot;2019-12-26&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1004,</span><br><span class="line">    &quot;name&quot;: &quot;imooc-4&quot;,</span><br><span class="line">    &quot;desc&quot;: &quot;imooc is good~！&quot;,</span><br><span class="line">    &quot;create_date&quot;: &quot;2019-12-27&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1005,</span><br><span class="line">    &quot;name&quot;: &quot;imooc-5&quot;,</span><br><span class="line">    &quot;desc&quot;: &quot;慕课网 is 强大！&quot;,</span><br><span class="line">    &quot;create_date&quot;: &quot;2019-12-28&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1006,</span><br><span class="line">    &quot;name&quot;: &quot;imooc-6&quot;,</span><br><span class="line">    &quot;desc&quot;: &quot;慕课是一个强大网站！&quot;,</span><br><span class="line">    &quot;create_date&quot;: &quot;2019-12-29&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1007,</span><br><span class="line">    &quot;name&quot;: &quot;imooc-7&quot;,</span><br><span class="line">    &quot;desc&quot;: &quot;慕课网是很牛网站！&quot;,</span><br><span class="line">    &quot;create_date&quot;: &quot;2019-12-30&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1008,</span><br><span class="line">    &quot;name&quot;: &quot;imooc-8&quot;,</span><br><span class="line">    &quot;desc&quot;: &quot;慕课网是很好看！&quot;,</span><br><span class="line">    &quot;create_date&quot;: &quot;2019-12-31&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1009,</span><br><span class="line">    &quot;name&quot;: &quot;imooc-9&quot;,</span><br><span class="line">    &quot;desc&quot;: &quot;在慕课网学习很久！&quot;,</span><br><span class="line">    &quot;create_date&quot;: &quot;2020-01-01&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>注：如果索引没有手动建立mappings，那么当插入文档数据的时候，会根据文档类型自动设置属性类型。这个就是es的动态映射，帮我们在index索引库中去建立数据结构的相关配置信息。</li>
<li>“fields”:{“type”:”keyword”} 对一个字段设置多种索引模式，使用text类型做全文检索，也可以使用keyword类型做聚合和排序</li>
<li>“ignore_above”:256 设置字段索引和存储的长度最大值，超过则被忽略</li>
</ul>
<h3 id="删除文档"><a href="#删除文档" class="headerlink" title="删除文档"></a>删除文档</h3><figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DELETE /my_doc/_doc/1</span><br></pre></td></tr></table></figure>
<ul>
<li>注：文档删除不是立即删除，文档还是保存在磁盘上，索引增长越来越多，才会把那些曾经标识过删除的，进行清理，从磁盘上移出去。</li>
</ul>
<h3 id="修改文档"><a href="#修改文档" class="headerlink" title="修改文档"></a>修改文档</h3><ul>
<li>局部：</li>
</ul>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">POST /my_doc/_doc/1/_update</span><br><span class="line">&#123;</span><br><span class="line">    &quot;doc&quot;:&#123;</span><br><span class="line">        &quot;name&quot;:&quot;wkq&quot;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>全量替换</li>
</ul>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">PUT /my_doc/_doc/1</span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;:1001,</span><br><span class="line">    &quot;name&quot;:&quot;imooc-1&quot;,</span><br><span class="line">    &quot;desc&quot;:&quot;imooc is very good,慕课网非常牛！&quot;,</span><br><span class="line">    &quot;create_date&quot;:&quot;2022-03-26&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>每次修改后，version会更改</li>
</ul>
<h3 id="文档的基本查询"><a href="#文档的基本查询" class="headerlink" title="文档的基本查询"></a>文档的基本查询</h3><ul>
<li>常规查询</li>
</ul>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">GET /index_demo/_doc/1</span><br><span class="line">GET /index_demo/_doc/_search</span><br></pre></td></tr></table></figure>
<ul>
<li>查询结构</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  &quot;_index&quot;: &quot;my_doc&quot;,</span><br><span class="line">  &quot;_type&quot;: &quot;_doc&quot;,</span><br><span class="line">  &quot;_id&quot;: &quot;2&quot;,</span><br><span class="line">  &quot;_score&quot;: &quot;1.0&quot;,</span><br><span class="line">  &quot;_version&quot;: &quot;9&quot;,</span><br><span class="line">  &quot;_source&quot;: &#123;</span><br><span class="line">    &quot;id&quot;: &quot;1002&quot;,</span><br><span class="line">    &quot;name&quot;: &quot;desc&quot;,</span><br><span class="line">    &quot;desc&quot;: &quot;imooc is fashion&quot;,</span><br><span class="line">    &quot;create_time&quot;: &quot;2022-03-26&quot;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>定制结果集</li>
</ul>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">GET /index_demo/_doc/1?_source=id,name</span><br><span class="line">GET /index_demo/_doc/_search?_source=id,name</span><br></pre></td></tr></table></figure>
<ul>
<li>判断文档是否存在</li>
</ul>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">HEAD /index_demo/_doc/1</span><br></pre></td></tr></table></figure>
<h3 id="元数据"><a href="#元数据" class="headerlink" title="元数据"></a>元数据</h3><ul>
<li><code>_index</code>:文档数据所属的那个索引，理解为数据库的某张表即可。</li>
<li><code>_type</code>:文档数据属于那个类型，新版本使用<code>_doc</code>。</li>
<li><code>_id</code>:文档数据的唯一标识，类似数据库中某张表的主键。可以自动生成或者手动指定</li>
<li><code>_score</code>:查询相关度，是否契合用户匹配，分数越高用户的搜索体验越高</li>
<li><code>_version</code>:版本号。</li>
<li><code>_source</code>:文档数据，json格式</li>
<li><code>_seq_no</code>: 文档版本号，作用同 <code>_version</code>(相当于学生编号，每个班级的班主任为学生分配编号，效率要比学校教务处分配来的更加高效，管理起来更加方便)</li>
<li><code>_primary_term</code>: 文档所在位置（相当于班级）</li>
</ul>
<h2 id="文档乐观锁控制-if-seq-no-与-if-primary-term"><a href="#文档乐观锁控制-if-seq-no-与-if-primary-term" class="headerlink" title="文档乐观锁控制 if_seq_no 与 if_primary_term"></a>文档乐观锁控制 <code>if_seq_no</code> 与 <code>if_primary_term</code></h2><ul>
<li>插入新数据</li>
</ul>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">POST /my_doc/_doc</span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1010,</span><br><span class="line">    &quot;name&quot;: &quot;imooc-1010&quot;,</span><br><span class="line">    &quot;desc&quot;: &quot;imoocimooc！&quot;,</span><br><span class="line">    &quot;create_date&quot;: &quot;2019-12-24&quot;</span><br><span class="line">&#125;</span><br><span class="line"># 此时 _version 为 1</span><br></pre></td></tr></table></figure>
<ul>
<li>修改数据</li>
</ul>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">POST    /my_doc/_doc/&#123;_id&#125;/_update</span><br><span class="line">&#123;</span><br><span class="line">    &quot;doc&quot;: &#123;</span><br><span class="line">        &quot;name&quot;: &quot;慕课&quot;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"># 此时 _version 为 2</span><br></pre></td></tr></table></figure>
<ul>
<li>模拟两个客户端操作同一个文档数据，<code>_version</code>都携带为一样的数值</li>
</ul>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"># 操作1</span><br><span class="line">POST    /my_doc/_doc/&#123;_id&#125;/_update?if_seq_no=&#123;数值&#125;&amp;if_primary_term=&#123;数值&#125;</span><br><span class="line">&#123;</span><br><span class="line">    &quot;doc&quot;: &#123;</span><br><span class="line">        &quot;name&quot;: &quot;慕课1&quot;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"># 操作2</span><br><span class="line">POST    /my_doc/_doc/&#123;_id&#125;/_update?if_seq_no=&#123;数值&#125;&amp;if_primary_term=&#123;数值&#125;</span><br><span class="line">&#123;</span><br><span class="line">    &quot;doc&quot;: &#123;</span><br><span class="line">        &quot;name&quot;: &quot;慕课2&quot;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="分词和分词器"><a href="#分词和分词器" class="headerlink" title="分词和分词器"></a>分词和分词器</h2><h3 id="什么是分词"><a href="#什么是分词" class="headerlink" title="什么是分词"></a>什么是分词</h3><p>把文本转换成为一个个的单词，分词称之为analysis。es默认只对英文语句做分词，中文不支持，每个中文字都会被拆分为独立的个体。</p>
<ul>
<li>英文分词：<code>I study in imooc.com</code></li>
<li>中文分词：<code>我在慕课网学习</code></li>
</ul>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">POST /_analyze</span><br><span class="line">&#123;</span><br><span class="line">    &quot;analyzer&quot;:&quot;standard&quot;,</span><br><span class="line">    &quot;text&quot;:&quot;text文本&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">POST /my_doc/_analyze</span><br><span class="line">&#123;</span><br><span class="line">    &quot;analyzer&quot;:&quot;standard&quot;,</span><br><span class="line">    &quot;field&quot;:&quot;name&quot;,</span><br><span class="line">    &quot;text&quot;:&quot;text文本&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="es内置分词器"><a href="#es内置分词器" class="headerlink" title="es内置分词器"></a>es内置分词器</h3><ul>
<li>standard：默认分词，单词会被拆分，大小会被转换为小写。</li>
<li>simple：按照非字母拆分。大写转为小写</li>
<li>whitespace：按照空格分词。忽略大小写</li>
<li>stop：去除无意义单词，比如 <code>the/a/an/is</code></li>
<li>keyword: 不做分词。把整个文本作文一个单独的关键词</li>
</ul>
<h3 id="建立IK中文分词器"><a href="#建立IK中文分词器" class="headerlink" title="建立IK中文分词器"></a>建立IK中文分词器</h3><p>Github：<a target="_blank" rel="noopener" href="https://github.com/medcl/elasticsearch-analysis-ik">https://github.com/medcl/elasticsearch-analysis-ik</a></p>
<ul>
<li>zip解压：unzip xxx.zip -d ik</li>
</ul>
<p>解压到elasticsearch的插件目录下</p>
<ul>
<li>ik_smart:智能分词：会做最粗粒度的拆分，适合Phrase查询</li>
<li>ik_max_word:最大分词：会将文本做最细粒度的拆分，穷尽各种可能的组合，适合Term Query</li>
</ul>
<h3 id="自定义中文词库"><a href="#自定义中文词库" class="headerlink" title="自定义中文词库"></a>自定义中文词库</h3><ol>
<li>在{es}/plugins/ik/config下，创建：</li>
</ol>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim custom.dic</span><br></pre></td></tr></table></figure>
<ol>
<li>并且添加内容：</li>
</ol>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">慕课网</span><br><span class="line">骚年</span><br></pre></td></tr></table></figure>
<ol>
<li>配置自定义扩展词典</li>
</ol>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;entry key=&quot;ext_dict&quot;&gt;custom.dic&lt;/entry&gt;</span><br></pre></td></tr></table></figure>
<ol>
<li>重启</li>
</ol>
<h2 id="ElasticSearch集群"><a href="#ElasticSearch集群" class="headerlink" title="ElasticSearch集群"></a>ElasticSearch集群</h2><p>集群不仅可以实现高可用，也能实现海量数据的横向扩展。</p>
<h3 id="分片机制"><a href="#分片机制" class="headerlink" title="分片机制"></a>分片机制</h3><p>每个索引可以被分片，就相当于吃披萨的时候被切了好几块，然后分给不同的人吃</p>
<ul>
<li>每个主分片都包含索引数据的一部分</li>
<li>replica shard 其实就是primary shard的一个备份，如果primary shard挂了，replica shard还是可以继续去访问的，这个时候就需要用到集群了。</li>
<li>同一个分片的主与副本是不会放在同一个服务器里的，因为一旦宕机，这个分片就没有了</li>
</ul>
<p><img src= "" data-lazy-src="ElasticSearch集群分片示意图.png" alt="ElasticSearch集群分片示意图"></p>
<ul>
<li>不管是服务器1、服务器2、服务器3，其中的一台宕机，集群仍然可以为用户提供完整的服务。</li>
</ul>
<h3 id="配置集群"><a href="#配置集群" class="headerlink" title="配置集群"></a>配置集群</h3><figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"># 配置集群名称，保证每个节点的名称相同，如此就能都处于一个集群之内了</span><br><span class="line">cluster.name: es</span><br><span class="line"></span><br><span class="line"># 每一个节点的名称，必须不一样</span><br><span class="line">node.name: node-1</span><br><span class="line"></span><br><span class="line"># http端口（使用默认即可）</span><br><span class="line">http.port: 9200</span><br><span class="line"></span><br><span class="line"># 主节点，作用主要是用于来管理整个集群，负责创建或删除索引，管理其他非master节点（相当于企业老总）</span><br><span class="line">node.master: true</span><br><span class="line"></span><br><span class="line"># 数据节点，用于对文档数据的增删改查</span><br><span class="line">node.data: true</span><br><span class="line"></span><br><span class="line"># 集群列表</span><br><span class="line">discovery.seed_hosts: [&quot;192.168.0.221&quot;, &quot;192.168.0.222&quot;, &quot;192.168.0.223&quot;]</span><br><span class="line"></span><br><span class="line"># 启动的时候使用一个master节点</span><br><span class="line">cluster.initial_master_nodes: [&quot;node-1&quot;]</span><br></pre></td></tr></table></figure>
<p>最后可以通过如下命令查看配置文件的内容：</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">more elasticsearch.yml | grep ~[~#]</span><br></pre></td></tr></table></figure>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">cluster.name: es-cluster</span><br><span class="line">node.name: node-3</span><br><span class="line">path.data: /usr/local/elasticsearch/data</span><br><span class="line">path.logs: /usr/local/elasticsearch/logs</span><br><span class="line">network.host: 0.0.0.0</span><br><span class="line">http.port: 9200</span><br><span class="line">http.cors.enabled: true</span><br><span class="line">http.cors.allow-origin: &quot;*&quot;</span><br><span class="line">node.master: true</span><br><span class="line">node.data: true</span><br><span class="line">discovery.seed_hosts: [&quot;192.168.0.221&quot;, &quot;192.168.0.222&quot;,&quot;192.168.0.223&quot;]</span><br><span class="line">cluster.initial_master_nodes: [&quot;node-1&quot;]</span><br></pre></td></tr></table></figure>
<h3 id="集群脑裂"><a href="#集群脑裂" class="headerlink" title="集群脑裂"></a>集群脑裂</h3><p><img src= "" data-lazy-src="集群脑裂.png" alt="集群脑裂"></p>
<ol>
<li><p>什么是脑裂？ 如果发生网络中断或者服务器宕机，那么集群会有可能被划分为两个部分，各自有自己的master来管理，那么这就是脑裂。</p>
</li>
<li><p>在新版本7.x中，minimum_master_node这个参数已经被移除了，这一块内容完全由ES自身去管理，这样就避免了脑裂的问题，选举也会非常快。</p>
</li>
</ol>
<h3 id="文档读写原理"><a href="#文档读写原理" class="headerlink" title="文档读写原理"></a>文档读写原理</h3><p>协调节点负责接收用户的请求</p>
<ul>
<li>写包含了文档的新增、修改、删除</li>
</ul>
<p><img src= "" data-lazy-src="文档写原理.png" alt="文档写原理"></p>
<ul>
<li>文档读原理</li>
</ul>
<p><img src= "" data-lazy-src="文档读原理.png" alt="文档读原理"></p>
<h2 id="DSL-结构化查询语言-搜索"><a href="#DSL-结构化查询语言-搜索" class="headerlink" title="DSL(结构化查询语言)搜索"></a>DSL(结构化查询语言)搜索</h2><h3 id="数据准备"><a href="#数据准备" class="headerlink" title="数据准备"></a>数据准备</h3><ul>
<li>自定义词库</li>
</ul>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">慕课网</span><br><span class="line">慕课</span><br><span class="line">课网</span><br><span class="line">慕</span><br><span class="line">课</span><br><span class="line">网</span><br></pre></td></tr></table></figure>
<ul>
<li>建立索引shop</li>
</ul>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">PUT /shop</span><br><span class="line">&#123;</span><br><span class="line">    &quot;settings&quot;:&#123;</span><br><span class="line">        &quot;number_of_shards&quot;:&quot;2&quot;,</span><br><span class="line">        &quot;number_of_replicas&quot;:&quot;1&quot;,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>手动建立mappings</li>
</ul>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_mapping</span><br><span class="line">&#123;</span><br><span class="line">    &quot;properties&quot;:&#123;</span><br><span class="line">        &quot;id&quot;:&#123;</span><br><span class="line">            &quot;type&quot;:&quot;long&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;age&quot;:&#123;</span><br><span class="line">            &quot;type&quot;:&quot;integer&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;username&quot;:&#123;</span><br><span class="line">            &quot;type&quot;:&quot;keyword</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;nickname&quot;:&#123;</span><br><span class="line">            &quot;type&quot;:&quot;text&quot;,</span><br><span class="line">            &quot;analyzer&quot;:&quot;ik_max_word&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;money&quot;:&#123;</span><br><span class="line">            &quot;type&quot;:&quot;float&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;desc&quot;:&#123;</span><br><span class="line">            &quot;type&quot;:&quot;text&quot;,</span><br><span class="line">            &quot;analyzer&quot;:&quot;ik_max_word&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;sex&quot;:&#123;</span><br><span class="line">            &quot;type&quot;:&quot;byte&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;birthday&quot;:&#123;</span><br><span class="line">            &quot;type&quot;:&quot;date&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;face&quot;:&#123;</span><br><span class="line">            &quot;type&quot;:&quot;text&quot;,</span><br><span class="line">            &quot;index&quot;:false</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>录入数据</li>
</ul>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br></pre></td><td class="code"><pre><span class="line">POST         /shop/_doc/1001</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1001,</span><br><span class="line">    &quot;age&quot;: 18,</span><br><span class="line">    &quot;username&quot;: &quot;imoocAmazing&quot;,</span><br><span class="line">    &quot;nickname&quot;: &quot;慕课网&quot;,</span><br><span class="line">    &quot;money&quot;: 88.8,</span><br><span class="line">    &quot;desc&quot;: &quot;我在慕课网学习java和前端，学习到了很多知识&quot;,</span><br><span class="line">    &quot;sex&quot;: 0,</span><br><span class="line">    &quot;birthday&quot;: &quot;1992-12-24&quot;,</span><br><span class="line">    &quot;face&quot;: &quot;https://www.imooc.com/static/img/index/logo.png&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1002,</span><br><span class="line">    &quot;age&quot;: 19,</span><br><span class="line">    &quot;username&quot;: &quot;justbuy&quot;,</span><br><span class="line">    &quot;nickname&quot;: &quot;周杰棍&quot;,</span><br><span class="line">    &quot;money&quot;: 77.8,</span><br><span class="line">    &quot;desc&quot;: &quot;今天上下班都很堵，车流量很大&quot;,</span><br><span class="line">    &quot;sex&quot;: 1,</span><br><span class="line">    &quot;birthday&quot;: &quot;1993-01-24&quot;,</span><br><span class="line">    &quot;face&quot;: &quot;https://www.imooc.com/static/img/index/logo.png&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1003,</span><br><span class="line">    &quot;age&quot;: 20,</span><br><span class="line">    &quot;username&quot;: &quot;bigFace&quot;,</span><br><span class="line">    &quot;nickname&quot;: &quot;飞翔的巨鹰&quot;,</span><br><span class="line">    &quot;money&quot;: 66.8,</span><br><span class="line">    &quot;desc&quot;: &quot;慕课网团队和导游坐飞机去海外旅游，去了新马泰和欧洲&quot;,</span><br><span class="line">    &quot;sex&quot;: 1,</span><br><span class="line">    &quot;birthday&quot;: &quot;1996-01-14&quot;,</span><br><span class="line">    &quot;face&quot;: &quot;https://www.imooc.com/static/img/index/logo.png&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1004,</span><br><span class="line">    &quot;age&quot;: 22,</span><br><span class="line">    &quot;username&quot;: &quot;flyfish&quot;,</span><br><span class="line">    &quot;nickname&quot;: &quot;水中鱼&quot;,</span><br><span class="line">    &quot;money&quot;: 55.8,</span><br><span class="line">    &quot;desc&quot;: &quot;昨天在学校的池塘里，看到有很多鱼在游泳，然后就去慕课网上课了&quot;,</span><br><span class="line">    &quot;sex&quot;: 0,</span><br><span class="line">    &quot;birthday&quot;: &quot;1988-02-14&quot;,</span><br><span class="line">    &quot;face&quot;: &quot;https://www.imooc.com/static/img/index/logo.png&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1005,</span><br><span class="line">    &quot;age&quot;: 25,</span><br><span class="line">    &quot;username&quot;: &quot;gotoplay&quot;,</span><br><span class="line">    &quot;nickname&quot;: &quot;ps游戏机&quot;,</span><br><span class="line">    &quot;money&quot;: 155.8,</span><br><span class="line">    &quot;desc&quot;: &quot;今年生日，女友送了我一台play station游戏机，非常好玩，非常不错&quot;,</span><br><span class="line">    &quot;sex&quot;: 1,</span><br><span class="line">    &quot;birthday&quot;: &quot;1989-03-14&quot;,</span><br><span class="line">    &quot;face&quot;: &quot;https://www.imooc.com/static/img/index/logo.png&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1006,</span><br><span class="line">    &quot;age&quot;: 19,</span><br><span class="line">    &quot;username&quot;: &quot;missimooc&quot;,</span><br><span class="line">    &quot;nickname&quot;: &quot;我叫小慕&quot;,</span><br><span class="line">    &quot;money&quot;: 156.8,</span><br><span class="line">    &quot;desc&quot;: &quot;我叫凌云慕，今年20岁，是一名律师，我在琦䯲星球做演讲&quot;,</span><br><span class="line">    &quot;sex&quot;: 1,</span><br><span class="line">    &quot;birthday&quot;: &quot;1993-04-14&quot;,</span><br><span class="line">    &quot;face&quot;: &quot;https://www.imooc.com/static/img/index/logo.png&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1007,</span><br><span class="line">    &quot;age&quot;: 19,</span><br><span class="line">    &quot;username&quot;: &quot;msgame&quot;,</span><br><span class="line">    &quot;nickname&quot;: &quot;gamexbox&quot;,</span><br><span class="line">    &quot;money&quot;: 1056.8,</span><br><span class="line">    &quot;desc&quot;: &quot;明天去进货，最近微软处理很多游戏机，还要买xbox游戏卡带&quot;,</span><br><span class="line">    &quot;sex&quot;: 1,</span><br><span class="line">    &quot;birthday&quot;: &quot;1985-05-14&quot;,</span><br><span class="line">    &quot;face&quot;: &quot;https://www.imooc.com/static/img/index/logo.png&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1008,</span><br><span class="line">    &quot;age&quot;: 19,</span><br><span class="line">    &quot;username&quot;: &quot;muke&quot;,</span><br><span class="line">    &quot;nickname&quot;: &quot;慕学习&quot;,</span><br><span class="line">    &quot;money&quot;: 1056.8,</span><br><span class="line">    &quot;desc&quot;: &quot;大学毕业后，可以到imooc.com进修&quot;,</span><br><span class="line">    &quot;sex&quot;: 1,</span><br><span class="line">    &quot;birthday&quot;: &quot;1995-06-14&quot;,</span><br><span class="line">    &quot;face&quot;: &quot;https://www.imooc.com/static/img/index/logo.png&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1009,</span><br><span class="line">    &quot;age&quot;: 22,</span><br><span class="line">    &quot;username&quot;: &quot;shaonian&quot;,</span><br><span class="line">    &quot;nickname&quot;: &quot;骚年轮&quot;,</span><br><span class="line">    &quot;money&quot;: 96.8,</span><br><span class="line">    &quot;desc&quot;: &quot;骚年在大学毕业后，考研究生去了&quot;,</span><br><span class="line">    &quot;sex&quot;: 1,</span><br><span class="line">    &quot;birthday&quot;: &quot;1998-07-14&quot;,</span><br><span class="line">    &quot;face&quot;: &quot;https://www.imooc.com/static/img/index/logo.png&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1010,</span><br><span class="line">    &quot;age&quot;: 30,</span><br><span class="line">    &quot;username&quot;: &quot;tata&quot;,</span><br><span class="line">    &quot;nickname&quot;: &quot;隔壁老王&quot;,</span><br><span class="line">    &quot;money&quot;: 100.8,</span><br><span class="line">    &quot;desc&quot;: &quot;隔壁老外去国外出差，带给我很多好吃的&quot;,</span><br><span class="line">    &quot;sex&quot;: 1,</span><br><span class="line">    &quot;birthday&quot;: &quot;1988-07-14&quot;,</span><br><span class="line">    &quot;face&quot;: &quot;https://www.imooc.com/static/img/index/logo.png&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1011,</span><br><span class="line">    &quot;age&quot;: 31,</span><br><span class="line">    &quot;username&quot;: &quot;sprder&quot;,</span><br><span class="line">    &quot;nickname&quot;: &quot;皮特帕克&quot;,</span><br><span class="line">    &quot;money&quot;: 180.8,</span><br><span class="line">    &quot;desc&quot;: &quot;它是一个超级英雄&quot;,</span><br><span class="line">    &quot;sex&quot;: 1,</span><br><span class="line">    &quot;birthday&quot;: &quot;1989-08-14&quot;,</span><br><span class="line">    &quot;face&quot;: &quot;https://www.imooc.com/static/img/index/logo.png&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;id&quot;: 1012,</span><br><span class="line">    &quot;age&quot;: 31,</span><br><span class="line">    &quot;username&quot;: &quot;super hero&quot;,</span><br><span class="line">    &quot;nickname&quot;: &quot;super hero&quot;,</span><br><span class="line">    &quot;money&quot;: 188.8,</span><br><span class="line">    &quot;desc&quot;: &quot;BatMan, GreenArrow, SpiderMan, IronMan... are all Super Hero&quot;,</span><br><span class="line">    &quot;sex&quot;: 1,</span><br><span class="line">    &quot;birthday&quot;: &quot;1980-08-14&quot;,</span><br><span class="line">    &quot;face&quot;: &quot;https://www.imooc.com/static/img/index/logo.png&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="请求参数的查询（Query-String）"><a href="#请求参数的查询（Query-String）" class="headerlink" title="请求参数的查询（Query String）"></a>请求参数的查询（Query String）</h3><p>查询<code>[字段]</code>包含<code>[内容]</code>的文档</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">GET /shop/_doc/_search?q=desc:慕课网</span><br><span class="line">GET /shop/_doc/_search?q=nickname:super&amp;q=age:25</span><br></pre></td></tr></table></figure>
<p>text与keyword搜索对比测试（keyword不会被倒排索引，不会被分词）</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">GET /shop/_doc/_search?q=nickname:super</span><br><span class="line">GET /shop/_doc/_search?q=username:super</span><br><span class="line">GET /shop/_doc/_search?q=username:super hero</span><br></pre></td></tr></table></figure>
<p>这种方式称之为QueryString查询方式，参数都是放在url中作为请求参数的。</p>
<h3 id="DSL基本语法"><a href="#DSL基本语法" class="headerlink" title="DSL基本语法"></a>DSL基本语法</h3><p>QueryString用得很少，一旦参数复杂就难以构建，所以大多数查询都会使用dsl来进行查询更好。</p>
<ul>
<li>Domain Specific Language</li>
<li>特定领域语言</li>
<li>基于JSON格式的数据查询</li>
<li>查询更灵活，有利于复杂查询</li>
</ul>
<p>DSL格式语法：</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"># 查询 _doc可以省略</span><br><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">        &quot;match&quot;:&#123;  //match会对搜索的内容进行一个分词的操作，先将查询关键字进行分词，分词以后再去拿这些词去搜索。</span><br><span class="line">            &quot;desc&quot;:&quot;慕课网&quot;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"># 判断某个字段是否存在</span><br><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">        &quot;exists&quot;:&#123;</span><br><span class="line">            &quot;field&quot;:&quot;desc&quot;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>语法格式为一个<code>json object</code> ,内容都是key-value键值对，json可以嵌套。</li>
<li>key可以是一些es的关键字，也可以是某个field字段，也可以是自定义名</li>
</ul>
<h3 id="查询所有与分页"><a href="#查询所有与分页" class="headerlink" title="查询所有与分页"></a>查询所有与分页</h3><h4 id="match-all"><a href="#match-all" class="headerlink" title="match_all"></a>match_all</h4><p>在索引中查询所有的文档</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GET /shop/_doc/_search</span><br></pre></td></tr></table></figure>
<p>或</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">GET /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">        &quot;match_all&quot;:&#123;&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;_source&quot;:[&quot;id&quot;,&quot;nickname&quot;,&quot;age&quot;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>Head可视化操作</li>
</ul>
<p><img src= "" data-lazy-src="Head可视化操作.jpg" alt="Head可视化操作"></p>
<h4 id="分页查询"><a href="#分页查询" class="headerlink" title="分页查询"></a>分页查询</h4><p>默认查询是只有10条记录，可以通过分页来展示</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">        &quot;match_all&quot;:&#123;&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;from&quot;:0, //开始位置</span><br><span class="line">    &quot;size&quot;:10 //显示条数</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>Head可视化操作-分页查询</li>
</ul>
<p><img src= "" data-lazy-src="Head可视化操作-分页查询.jpg" alt="Head可视化操作-分页查询"></p>
<h2 id="Logstash数据同步"><a href="#Logstash数据同步" class="headerlink" title="Logstash数据同步"></a>Logstash数据同步</h2><p>采集、转换、充实、然后输出</p>
<p>Logstash是elastic技术栈中的一个技术。它是一个数据采集引擎，可以从数据库采集数据到es中。我们可以通过设置自增id主键或者时间来控制数据的自动同步，这个id或者时间就是用于给logstash进行识别的</p>
<ul>
<li>id : 假设现在有1000条数据，Logstash识别后会进行一次同步，同步完会记录这个id为1000，以后数据库新增记录，那么id会一直累加，Logstash会有定时任务，发现有id大于1000了，则增量加入到es中。</li>
<li>update_time：同理，一开始同步1000条数据。每条数据都有一个字段，为time，初次同步完毕后，记录这个time，下次同步的时候进行时间比对，<br>如果有超过这个时间的，那么就可以做同步，这里可以同步新增数据，或者修改元数据，因为同一条数据的时间更改会被识别，而id则不会。</li>
</ul>
<p>官方链接：</p>
<blockquote>
<p>注意：使用Logstash的版本号与elasticsearch的版本号需要保持一致。</p>
</blockquote>
</article><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%88%86%E5%B8%83%E5%BC%8F/">分布式</a><a class="post-meta__tags" href="/tags/%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E/">搜索引擎</a></div><div class="post_share"><div class="social-share" data-image="/cover/ElasticSearch.jpg" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="/media/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">王开琦</div><div class="author-info__description">今日事今日毕</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">15</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E%E5%AF%B9%E6%AF%94"><span class="toc-number">1.</span> <span class="toc-text">搜索引擎对比</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ES%E6%A0%B8%E5%BF%83%E6%9C%AF%E8%AF%AD"><span class="toc-number">2.</span> <span class="toc-text">ES核心术语</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9B%86%E7%BE%A4%E7%9B%B8%E5%85%B3"><span class="toc-number">2.1.</span> <span class="toc-text">集群相关</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%80%E7%A7%B0"><span class="toc-number">2.2.</span> <span class="toc-text">简称</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%80%92%E6%8E%92%E7%B4%A2%E5%BC%95"><span class="toc-number">3.</span> <span class="toc-text">倒排索引</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B4%A2%E5%BC%95%E7%9A%84%E4%B8%80%E4%BA%9B%E6%93%8D%E4%BD%9C"><span class="toc-number">4.</span> <span class="toc-text">索引的一些操作</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9B%86%E7%BE%A4%E5%81%A5%E5%BA%B7"><span class="toc-number">4.1.</span> <span class="toc-text">集群健康</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E7%B4%A2%E5%BC%95"><span class="toc-number">4.2.</span> <span class="toc-text">创建索引</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E7%B4%A2%E5%BC%95"><span class="toc-number">4.3.</span> <span class="toc-text">查看索引</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E7%B4%A2%E5%BC%95"><span class="toc-number">4.4.</span> <span class="toc-text">删除索引</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B4%A2%E5%BC%95%E7%9A%84mappings%E6%98%A0%E5%B0%84"><span class="toc-number">5.</span> <span class="toc-text">索引的mappings映射</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B4%A2%E5%BC%95%E5%88%86%E8%AF%8D%E6%A6%82%E5%BF%B5"><span class="toc-number">5.1.</span> <span class="toc-text">索引分词概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E7%B4%A2%E5%BC%95%E7%9A%84%E5%90%8C%E6%97%B6%E5%88%9B%E5%BB%BAmappings"><span class="toc-number">5.2.</span> <span class="toc-text">创建索引的同时创建mappings</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E5%88%86%E8%AF%8D%E6%95%88%E6%9E%9C"><span class="toc-number">5.3.</span> <span class="toc-text">查看分词效果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%9D%E8%AF%95%E4%BF%AE%E6%94%B9-%E6%8A%A5%E9%94%99-%E5%A6%82%E6%9E%9C%E5%B1%9E%E6%80%A7%E5%B7%B2%E5%AD%98%E5%9C%A8%EF%BC%8C%E5%88%99%E4%B8%8D%E5%81%9A%E6%93%8D%E4%BD%9C"><span class="toc-number">5.4.</span> <span class="toc-text">尝试修改(报错,如果属性已存在，则不做操作)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BA%E5%B7%B2%E5%AD%98%E5%9C%A8%E7%9A%84%E7%B4%A2%E5%BC%95%E5%88%9B%E5%BB%BA%E6%88%96%E6%B7%BB%E5%8A%A0mappings"><span class="toc-number">5.5.</span> <span class="toc-text">为已存在的索引创建或添加mappings</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BB%E8%A6%81%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">5.6.</span> <span class="toc-text">主要数据类型</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E6%A1%A3%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="toc-number">6.</span> <span class="toc-text">文档的基本操作</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E6%96%87%E6%A1%A3%E4%B8%8E%E8%87%AA%E5%8A%A8%E6%98%A0%E5%B0%84"><span class="toc-number">6.1.</span> <span class="toc-text">添加文档与自动映射</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E6%96%87%E6%A1%A3"><span class="toc-number">6.2.</span> <span class="toc-text">删除文档</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9%E6%96%87%E6%A1%A3"><span class="toc-number">6.3.</span> <span class="toc-text">修改文档</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%87%E6%A1%A3%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%9F%A5%E8%AF%A2"><span class="toc-number">6.4.</span> <span class="toc-text">文档的基本查询</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%83%E6%95%B0%E6%8D%AE"><span class="toc-number">6.5.</span> <span class="toc-text">元数据</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E6%A1%A3%E4%B9%90%E8%A7%82%E9%94%81%E6%8E%A7%E5%88%B6-if-seq-no-%E4%B8%8E-if-primary-term"><span class="toc-number">7.</span> <span class="toc-text">文档乐观锁控制 if_seq_no 与 if_primary_term</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%86%E8%AF%8D%E5%92%8C%E5%88%86%E8%AF%8D%E5%99%A8"><span class="toc-number">8.</span> <span class="toc-text">分词和分词器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E5%88%86%E8%AF%8D"><span class="toc-number">8.1.</span> <span class="toc-text">什么是分词</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#es%E5%86%85%E7%BD%AE%E5%88%86%E8%AF%8D%E5%99%A8"><span class="toc-number">8.2.</span> <span class="toc-text">es内置分词器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BB%BA%E7%AB%8BIK%E4%B8%AD%E6%96%87%E5%88%86%E8%AF%8D%E5%99%A8"><span class="toc-number">8.3.</span> <span class="toc-text">建立IK中文分词器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%AD%E6%96%87%E8%AF%8D%E5%BA%93"><span class="toc-number">8.4.</span> <span class="toc-text">自定义中文词库</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ElasticSearch%E9%9B%86%E7%BE%A4"><span class="toc-number">9.</span> <span class="toc-text">ElasticSearch集群</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E7%89%87%E6%9C%BA%E5%88%B6"><span class="toc-number">9.1.</span> <span class="toc-text">分片机制</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E9%9B%86%E7%BE%A4"><span class="toc-number">9.2.</span> <span class="toc-text">配置集群</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9B%86%E7%BE%A4%E8%84%91%E8%A3%82"><span class="toc-number">9.3.</span> <span class="toc-text">集群脑裂</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%87%E6%A1%A3%E8%AF%BB%E5%86%99%E5%8E%9F%E7%90%86"><span class="toc-number">9.4.</span> <span class="toc-text">文档读写原理</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DSL-%E7%BB%93%E6%9E%84%E5%8C%96%E6%9F%A5%E8%AF%A2%E8%AF%AD%E8%A8%80-%E6%90%9C%E7%B4%A2"><span class="toc-number">10.</span> <span class="toc-text">DSL(结构化查询语言)搜索</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%87%86%E5%A4%87"><span class="toc-number">10.1.</span> <span class="toc-text">数据准备</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0%E7%9A%84%E6%9F%A5%E8%AF%A2%EF%BC%88Query-String%EF%BC%89"><span class="toc-number">10.2.</span> <span class="toc-text">请求参数的查询（Query String）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#DSL%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95"><span class="toc-number">10.3.</span> <span class="toc-text">DSL基本语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2%E6%89%80%E6%9C%89%E4%B8%8E%E5%88%86%E9%A1%B5"><span class="toc-number">10.4.</span> <span class="toc-text">查询所有与分页</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#match-all"><span class="toc-number">10.4.1.</span> <span class="toc-text">match_all</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%86%E9%A1%B5%E6%9F%A5%E8%AF%A2"><span class="toc-number">10.4.2.</span> <span class="toc-text">分页查询</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Logstash%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5"><span class="toc-number">11.</span> <span class="toc-text">Logstash数据同步</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E7%89%B9%E5%AE%9A%E8%AF%AD%E5%8F%A5%E7%9A%84%E5%8E%9F%E7%90%86%E5%92%8C%E8%B0%83%E4%BC%98/" title="3.数据库-数据库监控与调优-特定语句的原理和调优"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="3.数据库-数据库监控与调优-特定语句的原理和调优"/></a><div class="content"><a class="title" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E7%89%B9%E5%AE%9A%E8%AF%AD%E5%8F%A5%E7%9A%84%E5%8E%9F%E7%90%86%E5%92%8C%E8%B0%83%E4%BC%98/" title="3.数据库-数据库监控与调优-特定语句的原理和调优">3.数据库-数据库监控与调优-特定语句的原理和调优</a><time datetime="2022-04-22T08:14:26.000Z" title="发表于 2022-04-22 16:14:26">2022-04-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E6%95%B0%E6%8D%AE%E5%BA%93%E8%B0%83%E4%BC%98%E7%90%86%E8%AE%BA/" title="数据库调优理论"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="数据库调优理论"/></a><div class="content"><a class="title" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E6%95%B0%E6%8D%AE%E5%BA%93%E8%B0%83%E4%BC%98%E7%90%86%E8%AE%BA/" title="数据库调优理论">数据库调优理论</a><time datetime="2022-04-22T08:13:31.000Z" title="发表于 2022-04-22 16:13:31">2022-04-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E6%85%A2%E6%9F%A5%E8%AF%A2%E5%8F%91%E7%8E%B0%E4%B8%8E%E5%88%86%E6%9E%90/" title="慢查询发现与分析"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="慢查询发现与分析"/></a><div class="content"><a class="title" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E6%85%A2%E6%9F%A5%E8%AF%A2%E5%8F%91%E7%8E%B0%E4%B8%8E%E5%88%86%E6%9E%90/" title="慢查询发现与分析">慢查询发现与分析</a><time datetime="2022-04-22T08:12:32.000Z" title="发表于 2022-04-22 16:12:32">2022-04-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/1-Java-JVM%E8%B0%83%E4%BC%98%E7%90%86%E8%AE%BA/" title="JVM 调优理论"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JVM 调优理论"/></a><div class="content"><a class="title" href="/blog/1-Java-JVM%E8%B0%83%E4%BC%98%E7%90%86%E8%AE%BA/" title="JVM 调优理论">JVM 调优理论</a><time datetime="2022-04-21T06:40:41.000Z" title="发表于 2022-04-21 14:40:41">2022-04-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/20-%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86Eureka/" title="服务治理Eureka"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="服务治理Eureka"/></a><div class="content"><a class="title" href="/blog/20-%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86Eureka/" title="服务治理Eureka">服务治理Eureka</a><time datetime="2022-04-13T13:18:27.000Z" title="发表于 2022-04-13 21:18:27">2022-04-13</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="footer_custom_text">Copyright© 2022 blog.wkq.pub <span>陇ICP备2021003596号</span></a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>if (document.getElementsByClassName('mermaid').length) {
  if (window.mermaidJsLoad) mermaid.init()
  else {
    getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(() => {
      window.mermaidJsLoad = true
      mermaid.initialize({
        theme: 'default',
      })
      true && mermaid.init()
    })
  }
}</script></div><div class="aplayer no-destroy" data-id="7675104902" data-server="tencent" data-type="playlist" data-fixed="true" data-mini="true" data-listFolded="false" data-order="random" data-preload="none" data-autoplay="true" muted></div><canvas class="fireworks" mobile="true"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config-diff',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.removeEventListener('scroll', window.tocScrollFn)
  window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script></div><script src="/live2d_models/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2d_models/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2d_models/assets/penchan.model.json"},"display":{"position":"left","width":100,"height":150,"hOffset":-10,"vOffset":40},"mobile":{"show":true},"react":{"opacity":0.7},"log":false});</script></body></html>